// 03admin/12bizAuth/certAuth/certAuth.js

var co = require('../../../../resource/js/common.js')
var app = getApp()
var _this, _data,_opt
Page({
   /**
    * 页面的初始数据
    */
   data: {
      //认证照片
      biz_certImg: '',
      //图片数量
      imageCount: 0,
      //是否显示图片
      isShow: false,
      //数量
      IMG_NUM: 1,
      //成立时间
      approvedTime: '',
      //有效时间
      estiblishTime: '',
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      _this = this
      _data = this.data
      _opt = options
      //获取当前时间
      var curr_data = co.getNowFormatDate(1)
      this.setData({
         approvedTime: curr_data,
         estiblishTime: curr_data,
      })
   },

   onShow: function() {
      if (_data.imageCount == _data.IMG_NUM) {
         _this.setData({
            isShow: true
         })
      }
   },

   //成立时间选择器事件
   approvedTimeChange: function(e) {
      _this.setData({
         approvedTime: e.detail.value
      })
   },

   //有效时间选择器事件
   estiblishTimeChange: function(e) {
      _this.setData({
         estiblishTime: e.detail.value
      })
   },

   //选择图片
   chooseImage: function() {
      wx.chooseImage({
         count: 1, // 最多可以选择的图片张数，默认9
         sizeType: ['original'], // original 原图，compressed 压缩图，默认二者都有
         sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
         success: function(res) {
            console.log(res)
            if (res.tempFilePaths.length === 1) {
               var count = ++_this.data.imageCount;
               _this.setData({
                  imageCount: count,
                  isShow: true,
                  biz_certImg: res.tempFilePaths[0]
               })
            }
         },
      })
   },

   //删除图片
   deleteImage: function(e) {
      var count = --_this.data.imageCount;
      _this.setData({
         imageCount: count,
         isShow: false,
         biz_certImg: ''
      })
   },

   //图片上传(单张)
   uploadimg: function(path, param) {
      wx.uploadFile({
         url: 'https://api.jyhs.ls293.cn/api/Upload/img',
         filePath: path,
         name: 'image',
         formData: {
           dir: 'biz_auth/' + _opt.Biz_ID
         },
         success: function(res) {
            console.log('uploadimg:===', res)
            var obj = JSON.parse(res.data)
            if (obj.code == 0) {
               param.Auth_Photo = obj.data.wwwPath
               console.log(param)
               _this.uploadInfo(param)
            } else {
               wx.hideLoading()
               co.toastAllText(obj.msg)
            }
         }
      });
   },

   //上传信息
   uploadInfo: function(param) {
      co.getShareApi('biz/Bizmg/saveAuthInfo', param, function(res) {
         wx.hideLoading()
         co.toastAllText(res.msg)
         if (res.code == 0) {
            wx.navigateBack({})
         }
      })
   },

   //提交
   authSubmit: function(e) {
      console.log('e===', e)
      var info = e.detail.value
      if (_this.data.biz_certImg) {
         var param = {
            Biz_ID: _opt.Biz_ID,
            Name: info.Name,
            CreditCode: info.CreditCode,
            LegalPersonName: info.LegalPersonName,
            RegLocation: info.RegLocation,
            //ApprovedTime: info.ApprovedTime,
            //EstiblishTime: info.EstiblishTime,
            ApprovedTime: _data.approvedTime,
            EstiblishTime: _data.estiblishTime,
         }
         console.log('param',param)
         if (_this.checkValidity(param)) {
            wx.showLoading({
               title: '正在提交中...',
            })
            _this.uploadimg(_data.biz_certImg, param)
         }
      } else {
         co.toastAllText('必需提供营业执照照片!')
      }
   },

   //验证参数有效性
   checkValidity: function (param) {
      var err = ''
      if (co.empty(param.Name)) {
         err = '公司全名不能为空!'
      } else if (co.empty(param.CreditCode)) {
         err = '营业执照号不能为空!'
      } else if (co.empty(param.LegalPersonName)) {
         err = '法人名称不能为空!'
      } else if (co.empty(param.RegLocation)) {
         err = '公司地址不能为空!'
      } else if (co.empty(param.ApprovedTime)) {
         err = '成立时间不能为空!'
      } else if (co.empty(param.EstiblishTime)) {
         err = '有效时间不能为空!'
      } else if (!co.regExp(param.ApprovedTime, 6)) {
         err = '成立日期格式不正确!'
      } else if (!co.regExp(param.EstiblishTime, 6)) {
         err = '有效日期格式不正确!'
      }
      if (err != '') {
         co.toastAllText(err)
         return false
      }
      return true
   },

})